package userServlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import routeImplment.RouteImplment;
import ticketImplment.TicketImplment;
import userEntity.User;
import userImplment.UserImplement;



/**
 * Servlet implementation class Refund
 */
public class Refund extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Refund() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String refundTicketId1 = request.getParameter("RefundTicketId");
		int refundTicketId=Integer.parseInt(refundTicketId1);//获得需要退的票的Tid
		
		TicketImplment ticketImplment=new TicketImplment();
		HttpSession session=request.getSession();
		String Uname=ticketImplment.QueryTicketTid(refundTicketId).getUname();//通过session找到用户的名字Uname
		
		String Rname=ticketImplment.QueryTicketTid(refundTicketId).getRname();//通过需要退的票的Id找到票的信息，再通过票的信息找到路线的名字Rname
		UserImplement userImplement=new UserImplement();
		int id =userImplement.Query(Uname).getId();//通过用户名找到用户的信息，再找到用户的id
		
		
		RouteImplment routeImplment=new RouteImplment();
		int seatPrice=routeImplment.RouteQuery(Rname).getSeatPrice();//查询出路线票价
		int Rid=routeImplment.RouteQuery(Rname).getRid();//查询出路线Rid
		
		routeImplment.AddSeatNumber(Rid);//给指定路线座位加一
		
	
		userImplement.Pay(id,seatPrice);//退钱给用户
		
		
		User user;
		user=userImplement.Query(Uname);
		user.setBalance(userImplement.Query(id).getBalance());
		session.setAttribute("user",user);  //更新用户信息到sesion
		
		ticketImplment.DeleteTicket(refundTicketId);//删除指定Tid的票
		
		request.getRequestDispatcher("myTicket.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
